package com.yy.controller.mapper;

import com.yy.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddressBookMapper {
    /**
     * 根据userId查询地址列表
     * @param userId
     * @return
     */
    @Select("select * from address_book where user_id = #{userId} order by is_default desc")
    List<AddressBook> list(Long userId);

    /**
     * 修改地址信息
     * @param address
     */
    @Update("update address_book set user_id=#{userId},consignee=#{consignee}," +
            "sex=#{sex},phone=${phone},building=#{building}" +
            ",doorplate=#{doorplate},is_default=#{isDefault} where id=#{id}")
    void update(AddressBook address);

    /**
     * 添加地址
     * @param addressBook
     */
    @Insert("insert into address_book " +
            "(user_id, consignee, sex, phone, building, " +
            "doorplate, is_default) values (#{userId},#{consignee}," +
            "#{sex},#{phone},#{building},#{doorplate},#{isDefault})")
    void add(AddressBook addressBook);

    /**
     * 根据id删除地址
     * @param id
     */
    @Delete("delete from address_book where id=#{id}")
    void delete(Long id);

    /**
     * 通过id查询地址
     * @param id
     */
    @Select("select * from address_book where id=#{id}")
    AddressBook getById(Long id);
}
